package leetcode.string;

/**
 * @Description: https://leetcode.cn/problems/reverse-words-in-a-string-iii/
 * @Author Ammar
 * @Create 2023/8/4 19:30
 */
public class _557_反转字符串中的单词III {
    public String reverseWords(String s) {
        String[] split = s.split(" ");
        for (int i = 0; i < split.length; i++) {
//            split[i] = new StringBuffer(split[i]).reverse().toString();
            split[i] = reverse(split[i].toCharArray(), 0, split[i].length() - 1);
        }
        return String.join(" ", split);
    }

    public String reverse(char[] schars, int left, int right) {
        if (schars == null) return null;
        if (schars.length == 0) return "";
        if (right <= left) return new String(schars);
        while (left < right) {
            char tmp = schars[left];
            schars[left] = schars[right];
            schars[right] = tmp;
            left++;
            right--;
        }
        return new String(schars);
    }
}
